package com.thirtydays.frame.ui.text

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.blankj.utilcode.util.ScreenUtils
import com.google.android.flexbox.FlexboxLayout
import com.thirtydays.frame.databinding.FlowTextViewBinding
import com.thirtydays.frame.databinding.FlowTwoViewBinding
import com.thirtydays.frame.util.ext.clicks


/**
 * @author 小垚
 * @创建时间： 2020/11/26
 * @描述：
 **/
class FlowTwoView : FrameLayout {

    lateinit var mViewBinding: FlowTwoViewBinding
    lateinit var mFlowClick: OnFlowClick

    constructor(context: Context) : super(context) {
        initView(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView(context)
    }

    private fun initView(context: Context) {
        mViewBinding = FlowTwoViewBinding.inflate(LayoutInflater.from(context), this, true)
    }

    /**
     * list ：添加的内容
     * maxRow：最大行数
     * moreWidth：更多的宽度
     * horizontal: 左右的边距
     * moreView：更多的View
     * isAddFold:是否默认添加更多
     */
    fun addFlows(list: MutableList<String>, maxRow: Int, moreWidth: Float, horizontal: Float, moreView: View, isAddFold: Boolean = false) {
        var maxWidth = ScreenUtils.getScreenWidth() - moreWidth - horizontal
        var rows = 0
        var totalWidth = 0
        var isAddFolds = isAddFold
        mViewBinding.box.removeAllViews()
        list.forEachIndexed { index, s ->
            val inflate = FlowTextViewBinding.inflate(LayoutInflater.from(context), mViewBinding.box, false)
            inflate.tvValue.apply {
                text = s
            }

            if (this::mFlowClick.isInitialized) {
                inflate.tvValue.clicks {
                    mFlowClick.onClick(inflate.tvValue, index)
                }

                moreView.clicks {
                    mFlowClick.onMoreClick(moreView, index)
                }
            }
            mViewBinding.box.addView(inflate.root)

            inflate.root.post {
                if (inflate.root.left == 0) {
                    totalWidth = 0
                    rows++
                }
                var layoutParams = inflate.root.layoutParams as FlexboxLayout.LayoutParams
                totalWidth += inflate.root.width + layoutParams.leftMargin + layoutParams.rightMargin

                if (rows > maxRow) {
                    mViewBinding.box.removeView(inflate.root)
                } else if (rows == maxRow && (totalWidth >= maxWidth || inflate.root.width + layoutParams.rightMargin + layoutParams.leftMargin >= maxWidth)) {
                    mViewBinding.box.removeView(inflate.root)
                    isAddFolds = true
                }

                if (index == list.size - 1 && isAddFolds) {
                    mViewBinding.box.addView(moreView)
                }
            }
        }
    }

    interface OnFlowClick {
        fun onClick(view: View, index: Int)

        fun onMoreClick(view: View, index: Int)
    }
}